<?php

    preg_match( '/^\/edit(\/.*)/', $soc['url']['path'], $m );

    if ( is_file( $soc['dir'].'/.posts'.$m[1].'index.json' ) ) {
        $json = json_decode( file_get_contents( $soc['dir'].'/.posts'.$m[1].'index.json' ), true );
    } else {
        $json = array(
            'url'     => '',
            'title'   => '',
            'img'     => '',
            'html'    => '',
            'public'  => 'true',
        );
    }

    if ( $json['public'] === 'true' ) {
        $public_checked = 'checked';
    } else {
        $public_checked = '';
    }

?>
<html lang=<?php echo $config['lang']; ?>>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
<style>
<?php include('global.css'); ?>
<?php include('edit.css'); ?>
<?php if ( file_exists( "fix.css" ) ) { include( "fix.css" ); } ?>
</style>
</head>
    <body>
        <div class=info-box></div>
        <?php include('header.php'); ?>
        <main>
            <div class="new-post" data-new-post>

                <textarea class=title data-new-post-title rows=1 placeholder=Title oninput='this.style.height = "";this.style.height = (this.scrollHeight+2) + "px"'><?php echo $json['title']; ?></textarea>
                
                <div class="editable container" data-new-post-text><?php echo $json['html']; ?></div>
                
                <div class="container panel">
                    <form data-upload-form action=/api method=post>
                        <input type=hidden name=fn value=upload_files>
                        <input type=hidden name=attrs data-new-post-attrs>
                        <input type=hidden name=img data-new-post-img value="<?php echo $json['img']; ?>">
                        <input type=hidden name=url data-new-post-url value="<?php echo $json['url']; ?>">
                        <input id=upload-btn type=file name="myfile[]" multiple class=hide-upload-btn>
                        <label for=upload-btn>
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17">
                            <path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"></path>
                        </svg>
                        </label>
                        <div class="progress"> <div class="bar"></div > <div class="percent">0%</div > </div>
                        <label><input type=checkbox name=public data-new-post-public <?php echo $public_checked; ?>> <?php echo __( "Public" ); ?></label>
                    </form>

                    <button data-new-post-btn><?php echo __( "Save" ); ?></button>

                </div>
            </div>
        </main>
        <?php include('footer.php'); ?>
    </body>
</html>